// 管线流动效果  

// import * as turf from '@turf/turf'
var dlEllipse;
var polygonList = []
var feature_geojson = null;
var colorList = ['1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png', '8.png', '9.png', '10.png']
var colorArr = [];
let cesiumInit = {
	init(el, resp, imgList) {
		this.viewer = el;
		feature_geojson = resp
		colorArr = colorList.map(item => {
			return imgList + item
		})
		// this.loadData();
		/**
		 * 笛卡尔坐标转WGS84
		 * @param Cartesian3 单个点或点数组
		 */
		function Cartesian3ToWGS84(Cartesian3) {
			if (!Cartesian3 || !Cartesian3.x) {
				throw "Error in parameters";
			}
			var _cartesian3 = new Cesium.Cartesian3(Cartesian3.x, Cartesian3.y, Cartesian3.z);
			var _cartographic = Cesium.Cartographic.fromCartesian(_cartesian3);
			var _lat = Cesium.Math.toDegrees(_cartographic.latitude);
			var _lng = Cesium.Math.toDegrees(_cartographic.longitude);
			var _alt = _cartographic.height;
			return {
				lng: _lng,
				lat: _lat,
				alt: _alt
			};
		}


		var that = this;
		//根据绘制结果生成剖切面--地质体剖切
		this.drawLineString(function (positions) {
			var lastPositions = [];
			console.log("zuobiao");
			console.log(positions);
			for (var i = 0; i < positions.length; i++) {
				var jwdposition = Cartesian3ToWGS84(positions[i]);
				var feature_target = {
					"type": "Feature",
					"properties": {},
					"geometry": {
						"type": "Point",
						"coordinates": [jwdposition.lng, jwdposition.lat]
					}
				};
				var nearest = turf.nearestPoint(feature_target, feature_geojson);
				delete nearest.properties.distanceToPoint;
				delete nearest.properties.featureIndex;
				delete nearest.properties.x;
				delete nearest.properties.y;

				console.log("------------------------------>>>>>>>>>>>>>>>>");
				console.log(nearest);
				console.log("------------------------------>>>>>>>>>>>>>>>>");
				feature_target.properties = nearest.properties;
				console.log(feature_target);
				lastPositions.push(feature_target);
			}
			console.log(lastPositions);
			var levels = [];
			var polyGonPosArr = [];
			var valueArray1 = Object.values(lastPositions[0].properties);
			var lastZ=valueArray1[valueArray1.length-1]-130;
			var valueArray=[];
			debugger
			for (var j = 0; j <= valueArray1.length-1; j++) {
				valueArray.push(valueArray1[j]-lastZ);
				}
			console.log(valueArray);
			for (var j = 0; j <= valueArray.length-1; j++) {
				var leveltemp = [];
				var polyGonPosArrTemp = [];
				for (var z = 0; z < lastPositions.length-1; z++) {
					if (j == valueArray.length) {
						// debugger
						//  leveltemp.push(0.0);
					} else {
					
						var valueArray1 = Object.values(lastPositions[z].properties);
						leveltemp.push(valueArray[j]);
						polyGonPosArrTemp.push(lastPositions[z].geometry.coordinates[0]);
						polyGonPosArrTemp.push(lastPositions[z].geometry.coordinates[1]);
						polyGonPosArrTemp.push(0.0);
					}


				}
				if (polyGonPosArrTemp.length > 0) {
					polyGonPosArr.push(polyGonPosArrTemp);
				}
				levels.push(leveltemp);
			}
			console.log(levels);
			console.log(polyGonPosArr);

			//开始绘制面
			for (var i = 0; i < polyGonPosArr.length-1; i++) {
				var polygon = that.viewer.entities.add({
					wall: {
						positions: Cesium.Cartesian3.fromDegreesArrayHeights(polyGonPosArr[i]),
						maximumHeights: levels[i],
						minimumHeights: levels[i + 1],
						outline: false,
						// outlineColor: colorArr[i],
						outlineWidth: 4,
						// material: colorArr[i],
						material: new Cesium.ImageMaterialProperty({
							transparent: true,//设置透明
							image: colorArr[i],
							repeat: new Cesium.Cartesian2(1.0, 1.0)
						}),
						// Cesium.Color.fromRandom({
						// 	alpha: 1.0
						// }),
					},
				});
				polygonList.push(polygon)
				that.viewer.zoomTo(polygon);
			}

			// var entityIds = ["myEntity2", "myEntity1", "myEntity"];
			// that.viewer.entities.getById("myEntity").show = false;
		}, that.viewer);
		var rgba = Cesium.Color.fromCssColorString('#ffffff');




	},


	//绘制剖切线--地质体剖切
	drawLineString(callback, view) {
		this.viewer = view;
		var _this = this;
		var PolyLinePrimitive = (function () {
			function _(positions) {
				this.options = {
					polyline: {
						show: true,
						positions: [],
						material: Cesium.Color.RED,
						width: 3,
						// clampToGround: true
					}
				};
				this.positions = positions;
				this._init();
			}

			_.prototype._init = function () {
				var _self = this;
				var _update = function () {
					return _self.positions;
				};
				//实时更新polyline.positions
				this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
				var polyline = _this.viewer.entities.add(this.options);
				polygonList.push(polyline);
				polygonList.push(polyline);
				polygonList.push(polyline);
				// polyline.polyline.classificationType =Cesium.ClassificationType.CESIUM_3D_TILE;
			};
			return _;
		})();

		var handler = new Cesium.ScreenSpaceEventHandler(_this.viewer.scene.canvas);
		var positions = [];
		var poly = undefined;
		//鼠标左键单击画点
		handler.setInputAction(function (movement) {
			var cartesian = _this.viewer.scene.camera.pickEllipsoid(movement.position, _this.viewer.scene.globe.ellipsoid);
			cartesian = _this.viewer.scene.clampToHeight(cartesian);
			if (cartesian != undefined) {
				if (positions.length == 0) {
					positions.push(cartesian.clone());
				}
				positions.push(cartesian);
			}

		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
		//鼠标移动
		handler.setInputAction(function (movement) {
			var cartesian = _this.viewer.scene.camera.pickEllipsoid(movement.endPosition, _this.viewer.scene.globe.ellipsoid);

			cartesian = _this.viewer.scene.clampToHeight(cartesian);
			if (positions.length >= 2) {
				if (!Cesium.defined(poly)) {
					poly = new PolyLinePrimitive(positions);
				}
				else {
					if (cartesian != undefined) {
						positions.pop();
						// cartesian.y += (1 + Math.random());
						positions.push(cartesian);
					}
				}
			}
		}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
		//单击鼠标右键结束画线
		handler.setInputAction(function (movement) {
			handler.destroy();
			callback(positions);
			// _this.drawLineString(function(positions){
			// 	console.log("zuobiao");
			// 	console.log(positions);
			// },_this.viewer)
		}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
	},
	destroy() {
		const n = polygonList.length
		debugger
		for (let i = n; i > 0; i--) {
			this.viewer.entities.remove(polygonList[i]);
			polygonList.splice(i, 1)
		}

		// this.viewer.entities.removeAll();
		// this.viewer.imageryLayers.removeAll(true);
		// this.viewer.destroy();
	}
}
export default cesiumInit;
